﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SERP.StringMappings;
using SERP.DAO;
using SERP.Controllers;
namespace SERP.Modules.PersonTypes{
	public partial class PersonTypesList : SERP.Modules.ModuleBase
	{
		private PersonTypesController controller = new PersonTypesController();    
		protected void Page_Load(object sender, EventArgs e){
			LoadLanguage();
			if (!this.IsPostBack)
			{			
				LoadData();
			}     
		}
		private int GetPageIndex(){
			int pageIndex = 0;
			if (Request["PageIndex"] != null)
			{
				try{
					pageIndex = int.Parse(Request["PageIndex"]);
				}
				catch{
				}
			}
			return pageIndex;
		}
		private string GetFilter(){
			string strFilter="";
			if(txtPersonTypeId.Text.Trim()!=""){
				strFilter+=" AND PersonTypeId="+txtPersonTypeId.Text.Trim();
			}			
			if(txtTypeName.Text.Trim()!=""){
					strFilter+=" AND TypeName LIKE '%"+txtTypeName.Text.Trim()+"%'";
			}			
			if(txtNote.Text.Trim()!=""){
					strFilter+=" AND Note LIKE '%"+txtNote.Text.Trim()+"%'";
			}			
			if(strFilter.Length>0)
				strFilter=strFilter.Substring(4);
			return strFilter;
		}
		//Load All lable text
		private void LoadLanguage(){	
			labelPersonTypeId.Text=PersonTypesStringMapping.PersonTypeId+":";				
			validExpressionPersonTypeId.ErrorMessage=SERP.Constants.APP_REQUIRE_INTERGER_WARNING;			
			labelTypeName.Text=PersonTypesStringMapping.TypeName+":";				
			labelNote.Text=PersonTypesStringMapping.Note+":";				
			btnSearch.Text=SERP.Constants.APP_SEARCH;
			btnAddNew.Text=SERP.Constants.APP_ADDNEW;
			btnDelete.Text=SERP.Constants.APP_DELETE;
			btnHelps.Text=SERP.Constants.APP_HELPS;
            btnDelete.OnClientClick = "return confirm(\"" + Constants.APP_CONFIRM_DELELE + "\")";
			lblTitle.InnerText=SERP.Constants.APP_LIST+" "+PersonTypesStringMapping.PersonTypes;
		}
		private void LoadData(bool clearCache){
			//Load data to grid
			string filter=GetFilter();
			controller.LoadAllDSPersonTypes(clearCache);
			controller.Table.DefaultView.RowFilter=filter;
			gridData.DataSource=controller.Table;
			//gridData.PageIndex=GetPageIndex();
			gridData.DataBind();
		}
		private void LoadData()
		{
			LoadData(true);
		}
		private Dictionary<string, string> GetParameter(string stParam)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (string st in stParam.Split(','))
            {
                string[] arr = st.Split('=');
                dic.Add(arr[0], arr[1]);
            }
            return dic;
        }
		private void DeleteData(){
			for(int indexRow=0;indexRow<gridData.Items.Count;indexRow++){
				RepeaterItem row = gridData.Items[indexRow];
				CheckBox cbkRow=(CheckBox)row.FindControl("cbkPersonTypes");
				if(cbkRow!=null){
					if(cbkRow.Checked){
						LinkButton lkDelete=(LinkButton)row.FindControl("btnXoa");
                        Dictionary<string, string> dic = GetParameter(lkDelete.CommandArgument.ToString().Substring(1));
						controller.DeleteByID(Int32.Parse(dic["PersonTypeId"]));
					}
				}
			}
		}
		protected void btnSearch_Click(object sender, EventArgs e)
		{
			LoadData(false);
		}
		protected void OnAddNew(object sender, EventArgs e)
		{
			RedirectToPage("PersonTypesAddNew");
		}
		protected void OnDelete(object sender, EventArgs e)
		{
			DeleteData();	
			LoadData();
		}
		protected void btnXem_OnCommand(object sender,CommandEventArgs e){
			RedirectToPage("","","PersonTypesView",e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
		protected  void btnSua_OnCommand(object sender, CommandEventArgs e)
		{
			RedirectToPage("", "", "PersonTypesEdit", e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
		protected  void btnXoa_OnCommand(object sender, CommandEventArgs e)
		{
			RedirectToPage("", "", "PersonTypesDelete", e.CommandArgument.ToString().Trim().TrimStart('&'));
		}
	}
}
